﻿// CheckCaseTreeCtrl.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "CheckCaseTreeCtrl.h"


// CCheckCaseTreeCtrl

IMPLEMENT_DYNAMIC(CCheckCaseTreeCtrl, CTreeCtrl)

CCheckCaseTreeCtrl::CCheckCaseTreeCtrl()
{
	m_isEditable = FALSE;
	m_pCase = NULL;
}

CCheckCaseTreeCtrl::~CCheckCaseTreeCtrl()
{
	if (m_pCase)
	{
		m_pCase->Release();
		m_pCase = NULL;
	}
}


BEGIN_MESSAGE_MAP(CCheckCaseTreeCtrl, CTreeCtrl)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_NOTIFY_REFLECT(NM_CLICK, &CCheckCaseTreeCtrl::OnNMClick)
END_MESSAGE_MAP()



// CCheckCaseTreeCtrl 消息处理程序




int CCheckCaseTreeCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CTreeCtrl::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码

	return 0;
}


void CCheckCaseTreeCtrl::OnSize(UINT nType, int cx, int cy)
{
	CTreeCtrl::OnSize(nType, cx, cy);

	// TODO: 在此处添加消息处理程序代码
}


BOOL CCheckCaseTreeCtrl::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此添加专用代码和/或调用基类
	cs.style |= TVS_HASLINES | TVS_SHOWSELALWAYS | TVS_HASBUTTONS | TVS_NOHSCROLL | TVS_CHECKBOXES | TVS_EX_FADEINOUTEXPANDOS;

	return CTreeCtrl::PreCreateWindow(cs);
}


void CCheckCaseTreeCtrl::AttachBitmap()
{
	CBitmap bmp;
	if (bmp.LoadBitmap(IDB_BITMAP_CHECK_MODEL_TREE_16))
	{
		m_il.Create(16, 16, ILC_COLOR32 | ILC_MASK, 0, 0);
		m_il.Add(&bmp, RGB(255, 0, 255));
		int nCount = m_il.GetImageCount();
		for (int i = 1; i < nCount; i++)         //support 16
		{
			m_il.SetOverlayImage(i, i);
		}
		SetImageList(&m_il, TVSIL_NORMAL);
	}
}

void CCheckCaseTreeCtrl::SetCheckCase(ck::CheckCase* pCase)
{
	if (m_pCase)
	{
		m_pCase->Release();
		m_pCase = NULL;
	}
	m_pCase = pCase;
	if (m_pCase)
	{
		m_pCase->AddRef();
	}
}

void CCheckCaseTreeCtrl::Populate(BOOL bIsShowAll/* = FALSE*/)
{
	CString strTitile;
	strTitile.Format("%s检查项", m_pCase->GetName());

	this->DeleteAllItems();
	this->InsertItem(strTitile, CASE_TREE_ROOT_NODE, CASE_TREE_ROOT_NODE, TVI_ROOT);
	HTREEITEM hRoot = this->GetRootItem();
	this->SetCheck(hRoot, TRUE);

	ck::ElementRepository* pRepository = ck::ElementRepository::GetInstance();
	ck::CheckElement* pElement = NULL;
	ck::EnumCheckElement* pElements = NULL;
	pElements = pRepository->GetElements();
	if (pElements)
	{
		pElements->Reset();
		while ((pElement = pElements->Next()))
		{
			const char* szName = pElement->GetName();
			z_uint32 code = pElement->GetCode();

			HTREEITEM hItem = this->InsertItem(szName, CASE_TREE_CASE_NODE, CASE_TREE_CASE_NODE, hRoot);
			this->SetItemData(hItem, (DWORD)code);

			if (PopulateSubElement(code, hItem, bIsShowAll))
			{
				//该模型有检查项，如果Tree处于可编辑状态，需要设置CheckBox
				//this->SetCheck(hItem, TRUE);
				this->SetCheck(hItem, FALSE);
			}
			else
			{
				this->DeleteItem(hItem);
			}
		}
		pElements->Release();

		this->Expand(hRoot, TVE_EXPAND);
	}
}
/************************************************************************/
/* TRUE	：如果PopulateAllSubElement添加了子节点                          */
/* FALSE：如果PopulateAllSubElement没有子节点                            */
/************************************************************************/
BOOL CCheckCaseTreeCtrl::PopulateSubElement(z_uint32 nParentCode, HTREEITEM hParentItem, BOOL bIsShowAll)
{
	BOOL ret = FALSE;

	ck::ElementRepository* pRepository = ck::ElementRepository::GetInstance();
	ck::CheckElement* pElement = NULL;
	ck::EnumCheckElement* pElements = NULL;
	pElements = pRepository->GetChildElements(nParentCode);
	if (pElements)
	{
		pElements->Reset();
		while ((pElement = pElements->Next()))
		{
			const char* szName = pElement->GetName();
			z_uint32 code = pElement->GetCode();
			HTREEITEM hItem = this->InsertItem(szName, CASE_TREE_SUBE_NODE, CASE_TREE_SUBE_NODE, hParentItem);
			this->SetItemData(hItem, (DWORD)code);

			if (PopulateItem(code, hItem, bIsShowAll))
			{
				//该模型有检查项，如果Tree处于可编辑状态，需要设置CheckBox
				//if (m_isEditable)
				{
					//this->SetCheck(hItem, TRUE);
					this->SetCheck(hItem, FALSE);
				}
				ret = TRUE;
			}
			else
			{
				if (!bIsShowAll)
				{
					this->DeleteItem(hItem);
				}
			}

		}
		pElements->Release();

		this->Expand(hParentItem, TVE_EXPAND);
	}

	return ret;
}

BOOL CCheckCaseTreeCtrl::PopulateItem(z_uint32 nParentCode, HTREEITEM hParentItem, BOOL bIsShowAll)
{
	BOOL ret = FALSE;
	
	z_uint32 nModelCode = m_pCase->GetModeCode();

	ck::CheckModelManager* pModelManager = ck::CheckModelManager::GetInstance();
	ck::CheckCaseManager*  pCaseManager = ck::CheckCaseManager::GetInstance();

	ck::ElementRepository* pRepository = ck::ElementRepository::GetInstance();
	ck::CheckElement* pElement = NULL;
	ck::EnumCheckElement* pElements = NULL;
	pElements = pRepository->GetChildElements(nParentCode);

	if (pElements)
	{
		pElements->Reset();
		while ((pElement = pElements->Next()))
		{
			const char* szName = pElement->GetName();
			z_uint32 nItemCode = pElement->GetCode();
			z_uint32 nCaseCode = m_pCase->GetCode();

			if (pModelManager->HasItem(nModelCode, nItemCode))
			{
				ret = TRUE;
				HTREEITEM hItem = this->InsertItem(szName, CASE_TREE_ITEM_NODE, CASE_TREE_ITEM_NODE, hParentItem);
				this->SetItemData(hItem, (DWORD)nItemCode);

				//if (m_isEditable)
				{
					if (pCaseManager->FindCheckItem(nCaseCode, nItemCode))
					{
						this->SetCheck(hItem, TRUE);
						//this->SetCheck(hItem, FALSE);
					}
				}
			}
			else
			{
				if (bIsShowAll)
				{
					//如果全部显示的话，需要把其他的CheckItem也添加到Tree上
					HTREEITEM hItem = this->InsertItem(szName, CASE_TREE_ITEM_NODE, CASE_TREE_ITEM_NODE, hParentItem);
					this->SetItemData(hItem, (DWORD)nItemCode);
				}
			}

		}
		pElements->Release();

		this->Expand(hParentItem, TVE_EXPAND);
	}

	return ret;
}

int	CCheckCaseTreeCtrl::GetNodeType(HTREEITEM hItem)
{
	int nType = -1;
	HTREEITEM hRoot = GetRootItem();

	if (hRoot == hItem)
	{
		return CASE_TREE_ROOT_NODE;
	}
	else
	{
		HTREEITEM hParent = GetParentItem(hItem);
		if (hParent == hRoot)
		{
			return CASE_TREE_CASE_NODE;
		}
		else
		{
			hItem = hParent;
			hParent = GetParentItem(hItem);
			if (hParent == hRoot)
			{
				return CASE_TREE_SUBE_NODE;
			}
			else
			{
				hItem = hParent;
				hParent = GetParentItem(hItem);
				if (hParent == hRoot)
				{
					return CASE_TREE_ITEM_NODE;
				}
				else
				{
					hItem = hParent;
					hParent = GetParentItem(hItem);
					if (hParent == hRoot)
					{
						return CASE_TREE_OPER_NODE;
					}
				}
			}

		}
	}

	return nType;
}

z_uint32 CCheckCaseTreeCtrl::GetCheckElementCode(HTREEITEM hItem)
{
	return (z_uint32)(GetItemData(hItem));
}

void CCheckCaseTreeCtrl::OnNMClick(NMHDR *pNMHDR, LRESULT *pResult)
{
	// TODO: 在此添加控件通知处理程序代码
	CPoint pt;
	if (!GetCursorPos(&pt))
	{
		return;
	}

	this->ScreenToClient(&pt);
	UINT uFlag = 0;
	HTREEITEM hItem = this->HitTest(pt, &uFlag);
	if ((NULL == hItem) || !(TVHT_ONITEM & uFlag))
	{
		return;
	}
	this->SelectItem(hItem);

	//AfxMessageBox(this->GetItemText(hItem));


	*pResult = 0;
}
